/**
 * 模态弹窗
 * @param {string} _obj       -  绑定事件对象
 * @param {string} _animation -  要执行动画
 * @param {string} show_h     -  显示高度
 * @param {string} hide_h     -  收起高度
 */
const showModal = (that, _obj, _animation, show_h = '310rpx', hide_h = '0', setType = 'height') => {
	let animation = wx.createAnimation({
		duration: 200,
		timingFunction: "ease-in-out",
		delay: 0
	})

	if (!that.data[_obj]) {
		animation[setType](show_h).step()
		that.setData({
			[_animation]: animation.export(),
			[_obj]: true
		})
	} else {
		animation[setType](hide_h).step()
		that.setData({
			[_animation]: animation.export(),
			[_obj]: false
		})
	}
}

// 本地日期格式化 return Y-M-D
const getNowFormatDate = () => {
	var date = new Date();
	var seperator1 = "-";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = year + seperator1 + month + seperator1 + strDate;
	return currentdate;
}

// 本地时间格式化 return h m s
const formatTimeN = date => {
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [hour, minute, second].map(formatNumber).join(':')
}

/**
 * 文字换行
 * @param {Object} ctx        -  canvas对象.
 * @param {string} name       -  要处理的文字
 * @param {string} nameWidth  -  标题宽
 * @param {string} maxWidth   -  行最大宽度
 * @param {string} startX     -  行开始
 * @param {string} srartY     -  行结束
 * @param {string} wordsHight -  行高
 */
const wordsWrap = (ctx, name, nameWidth, maxWidth, startX, srartY, wordsHight) => {
	let lineWidth = 0;
	let lastSubStrIndex = 0;
	for (let i = 0; i < name.length; i++) {
		lineWidth += ctx.measureText(name[i]).width;
		if (lineWidth > maxWidth) {
			ctx.fillText(name.substring(lastSubStrIndex, i), startX, srartY);
			srartY += wordsHight;
			lineWidth = 0;
			lastSubStrIndex = i;
		}
		if (i == name.length - 1) {
			ctx.fillText(name.substring(lastSubStrIndex, i + 1), startX, srartY);
		}
	}
}

/*获取扫码后地址的后缀参数*/
const getQueryString = (url, name) => {
	var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
	var r = url.substr(1).match(reg)
	if (r != null) {
		return r[2]
	}
	return null;
}

/**
 * 中文字符串超长度加省略号（...）处理
 * @param {string} str - 进行处理的字符串，可含汉字
 * @param {string} len - 显示中文字个数，两个英文字母相当于一个汉字。
 */
const beautySub = (str, len) => {
	var reg = /[\u4e00-\u9fa5]/g, //专业匹配中文
		slice = str.substring(0, len),
		chineseCharNum = (~~(slice.match(reg) && slice.match(reg).length)),
		realen = slice.length * 2 - chineseCharNum;
	return str.substr(0, realen) + (realen < str.length ? "..." : "");
}

// 本地时间戳(10位数)
let timest = () => {
	var tmp = Date.parse(new Date()).toString();
	tmp = tmp.substr(0, 10);
	return tmp;
}

const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

module.exports = {
	showModal,
	getNowFormatDate,
	wordsWrap,
	formatTimeN,
	getQueryString,
	beautySub,
	timest
}